<?php
namespace Home\Controller;
use Think\Controller;
		/**
		 * 运营商控制器
		 */
		class YunyingController extends HomeController{
			/**
			 * 运营商列表
			 */
			public  function index(){
		/*		$page=I('get.page','1','strip_tags');
			$pagearr= getPageArr($page,C('PAGESIZE'),$count);*/

		$count=M('yunying_user')->count();
		$page=I('get.page','1','strip_tags');
		$pagearr= getPageArr($page,C('PAGESIZE'),$count);
		//$where['isdel']=0;
		$re=M('yunying_user')->where($where)->order('yid desc')->limit($pagearr['start'],$pagearr['pagesize'])->select();
		
	foreach ($re as $key => $value) {
		if($value['isdel']==1)
		{
           $re[$key]['frozen']='已冻结';
		}else
		{
          $re[$key]['frozen']='冻结';
		}
		if($value['rank']==1){
			$re[$key]['rank']='省代理';
			$re[$key]['dress']=M('area')->where("region_id='%d'",$value['sheng_id'])->getField("region_name");
		}elseif($value['rank']==2){
			$re[$key]['rank']='市代理';
			$dress='';
			$dress.=M('area')->where("region_id='%d'",$value['sheng_id'])->getField("region_name");
			$dress.=M('area')->where("region_id='%d'",$value['shi_id'])->getField("region_name");
			$re[$key]['dress']=$dress;
		}else{
			$re[$key]['rank']='县代理';
			$dress='';
			$dress.=M('area')->where("region_id='%d'",$value['sheng_id'])->getField("region_name");
			$dress.=M('area')->where("region_id='%d'",$value['shi_id'])->getField("region_name");
			$dress.=M('area')->where("region_id='%d'",$value['area_id'])->getField("region_name");

			$re[$key]['dress']=$dress;

		}

	}
		
	
		$this->assign('re',$re);
		$this->assign('pagearr',$pagearr);
		$this->display('index');

			}

	/**
	 * 运营商的添加
	 */
	public function add(){
		if(IS_POST){
		
			$yun=M('yunying_user');
			$yun->create();
			$yun->yname=I('post.yname','','strip_tags');
			$yun->companyname=I('post.companyname','','strip_tags');
			$yun->connectname=I('post.connectname','','strip_tags');
			$yun->mobile=I('post.mobile','','strip_tags');
			$yun->qq=I('post.qq','','strip_tags');
			///$yun->area=I('post.area','','strip_tags');
			$yun->address=I('post.address','','strip_tags');
			$yun->pid=I('post.pid',0,'strip_tags');///获取该运营商属于哪个运营商
			$sheng=I('post.sheng_id','','strip_tags');///获取省
			$shi=I('post.shi_id','','strip_tags');///获取市
			$qy=I('post.area_id','','strip_tags');//获取区域

			if(!empty($qy)){
				
				$yun->rank='3';///县级代理
				$yun->sheng_id=$sheng;
				$yun->shi_id=$shi;
				$yun->area_id=$shi;
				
			}elseif(!empty($shi)){
				$yun->rank='2';///市级代理
				$yun->sheng_id=$sheng;
				$yun->shi_id=$shi;
				
			}else{
				$yun->rank='1';//省级代理
				$yun->sheng_id=$sheng;
			}

			$yun->password=!empty($_POST['pass'])?md5($_POST['pass']):'';
			
			$result=$yun->add();

			
			if(!empty($result))
			{
				$this->success('添加成功');
				return '';
			}
			$this->error('添加失败');

				}else{
					////选择省
					$where['parent_id']=1;
					$city=M('area')->where($where)->getField("region_id,region_name");
					$this->assign('city',$city);
					$this->display();
				}
			}

	/**
	 * Ajax检测运营商的唯一性
	 * @param  companyname 公司名称
	 * @param  yname  运营商登录用户名
	 * return json
	 */


	public function weiyi()
	{

		$yun=M('yunying_user');
		$action=I('post.action','','strip_tags');
		switch ($action)
		 {
			case 'edityun':
				$yid=I('post.yid',0,'strip_tags');

				$where['companyname']=I('post.companyname','','strip_tags');

				$where['yid']=array('neq',$yid);

				$map['yname']=I('post.yname','','strip_tags');
				
				$map['yid']=array('neq',$yid);
			
				$result1=$yun->where($where)->count();

				$result2=$yun->where($map)->count();

				break;
			
			case 'addyun':
				$where['companyname']=I('post.companyname','','strip_tags');
				$map['yname']=I('post.yname','','strip_tags');
				$result1=$yun->where($where)->count();
				$result2=$yun->where($map)->count();
				break;
				
		}
		if($result1 > 0 || $result2 > 0)
		{
					$res['status']=0;
					$res['msg']='登录用户名或公司名称被占用';
					echo json_encode($res,JSON_UNESCAPED_UNICODE);
					return '';
		}
		$res['status']=1;
		echo json_encode($res,JSON_UNESCAPED_UNICODE);
	}


	/**
	 * 运营商冻结
	 */
	public function frozen(){
		$yid=I('post.yid',0,'strip_tags');
		$yun=M('yunying_user');
		$shop=M('shop_user');
		$item=M('item');
		$yun->startTrans();
		$data['isdel']=1;
		//修改运营商的状态0:激活，1：冻结
		$result=$yun->where('yid=%d',$yid)->save($data);
		
		//冻结此运营商下的商家		
		$map['isdel']=1;
		$shopnum=$shop->where('founder=%d',$yid)->count();
		if(!empty($shopnum))
		{
			$result2=$shop->where('founder=%d',$yid)->save($map);
		}else{
			$result2=1;
		}
		
		//冻结运营商下的商品
		$con['status']=2;
		$itemcount=$item->where('yid=%d',$yid)->count();
		if(!empty($itemcount))
		{
			$result3=$item->where('yid=%d',$yid)->save($con);
		}else{
			$result3=1;
		}
		if ($result !==false && $result2 !==false && $result3!== false)
		{
				$yun->commit();
				$message['status']=1;
				$message['msg']='操作成功';
				echo json_encode($message,JSON_UNESCAPED_UNICODE);
				return '';
		}else{
			$yun->rollback();
			$message['status']=0;
			$message['msg']='系统繁忙，操作失败';
			echo json_encode($message,JSON_UNESCAPED_UNICODE);
			return '';
		}

		
		

	}


	/**
	 * 运营商编辑
	 */
	public function edit(){
		if(IS_POST){
			$where['yid']=$_GET['id'];
			$yun=M('yunying_user');
			$yun->create();
			$yun->yname=I('post.yname','','strip_tags');
			$yun->companyname=I('post.companyname','','strip_tags');
			$yun->connectname=I('post.connectname','','strip_tags');
			$yun->mobile=I('post.mobile','','strip_tags');
			$yun->qq=I('post.qq','','strip_tags');
			$yun->area=I('post.area','','strip_tags');
			$yun->address=I('post.address','','strip_tags');
            $sheng=I('post.sheng_id','','strip_tags');///获取省
			$shi=I('post.shi_id','','strip_tags');///获取市
			$qy=I('post.area_id','','strip_tags');//获取区域

			if(!empty($qy)){
				/**/
				$yun->rank='3';///县级代理
				$yun->sheng_id=$sheng;
				$yun->shi_id=$shi;
				$yun->area_id=$qy;
				
			}elseif(!empty($shi)){
				$yun->rank='2';///市级代理
				$yun->sheng_id=$sheng;
				$yun->shi_id=$shi;
			}else{
				$yun->rank='1';//省级代理
				$yun->sheng_id=$sheng;
			}
			$pass=I('post.pass','','strip_tags');

			if (!empty($pass))
			{
				
				$yun->password=md5($pass);

			}
			
			$result=$yun->where($where)->save();
		
			if($result !== false){
				
				echo "<script>alert('修改成功');location.href='/admin.php/Home/Yunying/index'</script>";
				return '';
				
			}else
			{
				echo "<script>alert('请重新修改');location.href='/admin.php/Home/Yunying/index'</script>";
				return '';
			}

			}else{
				$where['yid']=$_GET['id'];
				$re=M('yunying_user')->where($where)->find();
                  $re['sheng']=M('area')->where("region_id='%d'",$re['sheng_id'])->getField("region_id,region_name");
                  $re['shi']=M('area')->where("region_id='%d'",$re['shi_id'])->getField("region_id,region_name");
                  $re['area']=M('area')->where("region_id='%d'",$re['area_id'])->getField("region_id,region_name");
                  ////选择省
					$where['parent_id']=1;
					$city=M('area')->where($where)->getField("region_id,region_name");
					$this->assign('city',$city);
					///选择市
					$district=M('area')->where("parent_id='%d'",array_keys($re['sheng']))->getField("region_id,region_name");
					$this->assign('district',$district);
					///选择区域
					$area=M('area')->where("parent_id='%d'",array_keys($re['shi']))->getField("region_id,region_name");
					$this->assign('area',$area);
                
				$this->assign('re',$re);
				$this->display();
			}
		}


		/**
		 * 运营商下的加盟商
		 */
		public function minute()
		{
			$where['founder']=$_GET['id'];
			$count=M('shop_user')->where($where)->count();
			$page=I('get.page','1','strip_tags');
			$pagearr= getPageArr($page,C('PAGESIZE'),$count);
			$re=M('shop_user')->where($where)->limit($pagearr['start'],$pagearr['pagesize'])->select();
			$this->assign('re',$re)->assign('pagearr',$pagearr);
			$this->display();
		}
		/*
		 *搜索运营商
		 *keywords 运营商关键词
		 *return json
		 */
		public function searYun(){
			
			$arg=array();
			$arg['keywords']=I('post.keywords','','strip_tags');
			
			$yunying=D('YunyingUser');

			$users=$yunying->index($arg);

			$result=array();

			$result['status']=!empty($users)?1:0;

			$result['users']=$users;

			echo json_encode($result);

			return '';
		}
		/**
		 * 三级联动市区选择
		 */
		public function city(){

			$where['parent_id']=I('get.province','','strip_tags');
		$result=M('area')->where($where)->getField("region_id,region_name");
		$str='';
		foreach($result as $k=>$v){
		  $str.= '<option value='.$k.'>'.$v.'</option>';

		}
		echo $str;

		}
	/**
	 *三级联动获取区域
	 */
	public function area(){
		$where['parent_id']=I('get.region','','strip_tags');
		$result=M('area')->where($where)->getField("region_id,region_name");
		$str='';
		foreach($result as $k=>$v){
		  $str.= '<option value='.$k.'>'.$v.'</option>';

		}
		echo $str;

	}
	/**
	 * 解冻运营商
	 */
	public function Thaw()
	{
      $yid=I('post.yid',0,'strip_tags');
		$yun=M('yunying_user');
		$shop=M('shop_user');
		$item=M('item');
		$yun->startTrans();
		$data['isdel']=0;
		//修改运营商的状态0:激活，1：冻结
		$result=$yun->where('yid=%d',$yid)->save($data);
		
		//冻结此运营商下的商家		
		$map['isdel']=0;
		$shopnum=$shop->where('founder=%d',$yid)->count();
		if(!empty($shopnum))
		{
			$result2=$shop->where('founder=%d',$yid)->save($map);
		}else{
			$result2=1;
		}
		
		//冻结运营商下的商品
		$con['status']=1;
		$itemcount=$item->where('yid=%d',$yid)->count();
		if(!empty($itemcount))
		{
			$result3=$item->where('yid=%d',$yid)->save($con);
		}else{
			$result3=1;
		}
		if ($result !==false && $result2 !==false && $result3!== false)
		{
				$yun->commit();
				$message['status']=1;
				$message['msg']='操作成功';
				echo json_encode($message,JSON_UNESCAPED_UNICODE);
				return '';
		}else{
			$yun->rollback();
			$message['status']=0;
			$message['msg']='系统繁忙，操作失败';
			echo json_encode($message,JSON_UNESCAPED_UNICODE);
			return '';
		}

		 
	}

 }